package com.py.credit.service.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.py.credit.common.rest.RestServiceResult;
import com.py.credit.dao.DataMapCollectionDao;
import com.py.credit.dao.DoublePublicityDao;
import com.py.credit.entity.SortDoubleData;
import com.py.credit.entity.ZrrcfDouble;
import com.py.credit.entity.ZrrxkDouble;
import com.py.credit.service.DoublePublicityService;
import com.py.credit.utils.NumberUtils;
import com.py.framework.core.common.Page;

@Service
public class DoublePublicityServiceImpl implements DoublePublicityService {

	@Autowired
	private DataMapCollectionDao dataMapCollectionDao;

	@Autowired
	private DoublePublicityDao doublePublicityDao;

	@Override
	public List<Object[]> xzxk() {
		// TODO Auto-generated method stub
		return doublePublicityDao.xzxk();
	}

	@Override
	public Map xzxkxyzt() {
		// TODO Auto-generated method stub
		Map m = new HashMap();
		long countZrrXzxk = doublePublicityDao.countZrrXzxk();
		long countFrXzxk = doublePublicityDao.countFrXzxk();
		if (countZrrXzxk == 0 && countFrXzxk == 0) {
			m.put("countZrrXzxk", 0);
			m.put("countFrXzxk", 0);
			m.put("rateZrrXzxk", 0);
			m.put("rateFrXzxk", 0);
		} else {
			double rateZrrXzxk = (double) countZrrXzxk / (countFrXzxk + countZrrXzxk);
			rateZrrXzxk = new BigDecimal(rateZrrXzxk).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
			double rateFrXzxk = (double) countFrXzxk / (countFrXzxk + countZrrXzxk);
			rateFrXzxk = new BigDecimal(rateFrXzxk).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
			m.put("countZrrXzxk", countZrrXzxk);
			m.put("countFrXzxk", countFrXzxk);
			m.put("rateZrrXzxk", rateZrrXzxk);
			m.put("rateFrXzxk", rateFrXzxk);
		}
		m.put("data", doublePublicityDao.xzxkxyzt());
		return m;
	}

	@Override
	public List<Object[]> xzxktop() {
		// TODO Auto-generated method stub
		return doublePublicityDao.xzxktop();
	}

	@Override
	public List<Object[]> doublePublicityRate() {
		// TODO Auto-generated method stub
		return doublePublicityDao.doublePublicityRate();
	}

	@Override
	public List<Object[]> xzcf() {
		// TODO Auto-generated method stub
		return doublePublicityDao.xzcf();
	}

	@Override
	public Map xzcfxyzt() {
		Map m = new HashMap();
		long countZrrXzcf = doublePublicityDao.countZrrXzcf();
		long countFrXzcf = doublePublicityDao.countFrXzcf();
		if (countZrrXzcf == 0 && countFrXzcf == 0) {
			m.put("countZrrXzcf", 0);
			m.put("countFrXzcf", 0);
			m.put("rateZrrXzcf", 0);
			m.put("rateFrXzcf", 0);

		} else {
			double rateZrrXzcf = (double) countZrrXzcf / (countFrXzcf + countZrrXzcf);
			rateZrrXzcf = new BigDecimal(rateZrrXzcf).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
			double rateFrXzcf = (double) countFrXzcf / (countFrXzcf + countZrrXzcf);
			rateFrXzcf = new BigDecimal(rateFrXzcf).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
			m.put("countZrrXzcf", countZrrXzcf);
			m.put("countFrXzcf", countFrXzcf);
			m.put("rateZrrXzcf", rateZrrXzcf);
			m.put("rateFrXzcf", rateFrXzcf);
		}
		m.put("data", doublePublicityDao.xzcfxyzt());
		return m;
	}

	@Override
	public List<Object[]> xzcftop() {
		// TODO Auto-generated method stub
		return doublePublicityDao.xzcftop();
	}

	@Override
	public List<Map<String, Object>> centralmapSingle() {
		List<Map<String, Object>> all = new ArrayList<Map<String, Object>>();
		List<LinkedHashMap> listCodes = dataMapCollectionDao.queryAllAreaCodes();
		for (int j = 0; j < listCodes.size(); j++) {
			// TODO Auto-generated method stub
			List<LinkedHashMap> list = new LinkedList<LinkedHashMap>();
			Map<String, Object> m = new HashMap<String, Object>();
			list = doublePublicityDao.centralmapSingle(String.valueOf(listCodes.get(j).get("code")));
			Long newCount = null;
			Long lastCount = null;
			m.put("qybm", listCodes.get(j).get("code"));
			m.put("qymc", listCodes.get(j).get("name"));
			for (int i = 0; i < list.size(); i++) {
				LinkedHashMap l = (LinkedHashMap) list.get(i);
				String type = String.valueOf(l.get("type"));
				if (type.equals("new")) {
					newCount = (Long) l.get("dataCount");
					m.put("newCount", newCount);
				} else {
					lastCount = (Long) l.get("dataCount");
					m.put("lastCount", lastCount);
				}
			}
			double rate;
			if (lastCount == 0 && newCount != 0) {
				rate = 100;
			} else if (lastCount == 0 && newCount == 0) {
				rate = 0;
			} else {
				rate = (double) (newCount - lastCount) / lastCount;
				rate = new BigDecimal(rate).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
			}
			m.put("rate", rate);
			all.add(m);
		}
		return all;
	}

	public static void main(String args[]) {
		double rate = (double) (400 - 1370) / 1370;
		rate = new BigDecimal(rate).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
		System.out.println(rate);
	}

	@Transactional
	@Override
	public void insertCentralMap() {
		List<LinkedHashMap> listCodes = dataMapCollectionDao.queryAllAreaCodes();
		for (int j = 0; j < listCodes.size(); j++) {
			// TODO Auto-generated method stub
			List<LinkedHashMap> list = new LinkedList<LinkedHashMap>();
			Map<String, Object> m = new HashMap<String, Object>();
			list = doublePublicityDao.centralmapSingle(String.valueOf(listCodes.get(j).get("code")));
			Long newCount = null;
			Long lastCount = null;
			for (int i = 0; i < list.size(); i++) {
				LinkedHashMap l = (LinkedHashMap) list.get(i);
				String type = String.valueOf(l.get("type"));
				if (type.equals("new")) {
					newCount = (Long) l.get("dataCount");
					m.put("newCount", newCount);
				} else {
					lastCount = (Long) l.get("dataCount");
					m.put("lastCount", lastCount);
				}
			}
			double rate;
			if (lastCount == 0 && newCount != 0) {
				rate = 100;
			} else if (lastCount == 0 && newCount == 0) {
				rate = 0;
			} else {
				rate = (double) (newCount - lastCount) / lastCount;
				rate = new BigDecimal(rate).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
				rate = rate * 100;
			}
			doublePublicityDao.insertCentralMap((String) listCodes.get(j).get("code"),
					(String) listCodes.get(j).get("name"), (Long) m.get("newCount"), (Long) m.get("lastCount"), rate);
		}

	}

	@Override
	public List<Map> centralmapAll() {
		// TODO Auto-generated method stub
		return doublePublicityDao.centralmapAll();
	}

	@Override
	public List<Map<String, Integer>> countPercentByMeasure() {

		return doublePublicityDao.countPercentByMeasure();
	}
	@Override
	public List<Map<String, Integer>> countPercentHhByMeasure() {

		return doublePublicityDao.countPercentHhByMeasure();
	}
	

	@Override
	public Long countDataByDate() {

		SimpleDateFormat d1 = new SimpleDateFormat("yyyy");
		SimpleDateFormat d2 = new SimpleDateFormat("MM");
		Date date = new Date();
		String year = d1.format(date);
		String month = d2.format(date);
		Map<String, Integer> map = new HashMap<>();
		map.put("year", Integer.valueOf(year));
		map.put("month", Integer.valueOf(month));
		return doublePublicityDao.countDataByDate(map);
	}
	@Override
	public Object countDataHhByDate() {
		SimpleDateFormat d1 = new SimpleDateFormat("yyyy");
		SimpleDateFormat d2 = new SimpleDateFormat("MM");
		Date date = new Date();
		String year = d1.format(date);
		String month = d2.format(date);
		Map<String, Integer> map = new HashMap<>();
		map.put("year", Integer.valueOf(year));
		map.put("month", Integer.valueOf(month));
		return doublePublicityDao.countDataHhByDate(map);
	}
	@Override
	public List<SortDoubleData> queryallDataByDeptName() {
		List<SortDoubleData> list = doublePublicityDao.queryallDataByDeptName();
		return list;
	}
	
	@Override
	public Object queryallHhDataByDeptName() {
		List<SortDoubleData> list = doublePublicityDao.queryallHhDataByDeptName();
		return list;
	}

	@Override
	public RestServiceResult<Object> queryDoubleDataForPage(String params1, String params2, Page page,
			String tableName) {
		RestServiceResult<Object> result = new RestServiceResult<Object>();
		if (tableName.equals("法人行政处罚")) {
			result.setData(doublePublicityDao.queryFrcfDataByA000001AndA000002ForPage(params1, page));
		} else if (tableName.equals("法人行政许可")) {
			result.setData(doublePublicityDao.queryFrxkDataByA000001AndA000002ForPage(params1, page));
		} else if (tableName.equals("自然人行政许可")) {
			
			List<ZrrxkDouble> list =doublePublicityDao.queryZrrxkDataByA000001AndA000002ForPage(params2, page);
			for(ZrrxkDouble value:list) {
				value.setB000002(NumberUtils.idEncrypt(value.getB000002()));			
			}
			result.setData(list);
		} else if (tableName.equals("自然人行政处罚")) {			
			List<ZrrcfDouble> list =doublePublicityDao.queryZrrcfDataByA000001AndA000002ForPage(params2, page);
			for(ZrrcfDouble value:list) {
				value.setB000002(NumberUtils.idEncrypt(value.getB000002()));			
			}
			result.setData(list);
		}else if(tableName.equals("frhei")) {
			result.setData(doublePublicityDao.selectHeifrByColumnForPage(params1, page,""));
		}else if(tableName.equals("frhon")) {
			result.setData(doublePublicityDao.selectHonfrByColumnForPage(params1, page,""));
		}else if(tableName.equals("jyycml")) {
			result.setData(doublePublicityDao.selectJyycmlByColumnForPage(params1, page,""));
		}
		return result;
	}

	@Override
	public RestServiceResult<Object> selectFrxkByColumnForPage(String params1, String params2, String tableName,
			Page page,String record_id) {

		RestServiceResult<Object> result = new RestServiceResult<Object>();
		if (tableName.equals("法人行政许可")) {
			result.setData(doublePublicityDao.selectFrxkByColumnForPage(params1, page,record_id));
		} else if (tableName.equals("自然人行政许可")) {
			result.setData(doublePublicityDao.selectZrrxkByColumnForPage(params2, page,record_id));
		} else if (tableName.equals("法人行政处罚")) {
			result.setData(doublePublicityDao.selectFrcfByColumnForPage(params1, page,record_id));
		} else if (tableName.equals("自然人行政处罚")) {
			result.setData(doublePublicityDao.selectZrrcfByColumnForPage(params2, page,record_id));
		}else if(tableName.equals("frhei")) {
			result.setData(doublePublicityDao.selectHeifrByColumnForPage(params1, page,record_id));
		}else if(tableName.equals("frhon")) {
			result.setData(doublePublicityDao.selectHonfrByColumnForPage(params1, page,record_id));
		}else if(tableName.equals("imp")) {
			result.setData(doublePublicityDao.selectImpByColumnForPage(params1, page,record_id));
		}else if(tableName.equals("jyycml")) {
			result.setData(doublePublicityDao.selectJyycmlByColumnForPage(params1, page,record_id));
		}
		return result;
	}

	@Override
	public List<Map> countSumByDate(String date) {
		// TODO Auto-generated method stub
		return doublePublicityDao.countSumByDate(date);
	}
	
	@Override
	public List<Map> countSumByDataAndDeptTop3(String date) {
		// TODO Auto-generated method stub
		return doublePublicityDao.countSumByDataAndDeptTop3(date);
	}
	
	@Override
	public Map countSumByDept(String date) {
		// TODO Auto-generated method stub
		Map m=new HashMap();
		Integer sum=doublePublicityDao.countSum(date);
		List<Map> countSumByDept=doublePublicityDao.countSumByDept(date);
		m.put("sum", sum);
		m.put("countSumByDept", countSumByDept);
		return m;
	}

	
	
	
	
	
}
